package org.example.lease.controller;

import org.example.lease.model.result.Result;
import org.example.lease.service.FileService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description 使用对象存储的时候的文件上传的技术步骤
 * 1.前端上传图片
 * 2.存储到MinIO
 * 3.返回MinIO图像URL
 * 4.保存公寓、房间等包含该图像URL字段的实体信息
 * 5.将带有图像URL字段的实体信息存储到MySQL
 * @date: 2025/5/16 16:10
 */
@Tag(name = "文件管理")
@Slf4j
@RequestMapping("/admin/file")
@RestController
public class FileUploadController {
    @Resource
    private FileService fileService;
    @Operation(summary = "上传图片接口")
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String URL = fileService.upload(file);
        return Result.ok(URL);
    }
}
